package com.ybwh.springboot2.controller;

import com.ybwh.springboot2.common.RespData;
import com.ybwh.springboot2.dao.AreaMapper;
import com.ybwh.springboot2.mapstruct.UserInfo;
import com.ybwh.springboot2.model.vo.AreaVo;
import com.ybwh.springboot2.util.FileDownUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author saytime
 * @since 2018-12-06
 */
@Slf4j
@Api(tags = "地区")
@Controller
@RequestMapping("/area")
public class AreaController {

    @Resource
    private AreaMapper areaMapper;


    @ResponseBody
    @RequestMapping(value = "/trim2", method = RequestMethod.GET)
    public RespData<Object> trim2(@ApiParam(value = "地区名称",required = true) @RequestParam("areaName") String areaName) {
        log.info("-------------areaName2={}", areaName);

        return RespData.successResp(null);
    }

    @ResponseBody
    @RequestMapping(value = "/trim1", method = RequestMethod.POST)
    public RespData<Object> trim1(@Validated @RequestBody AreaVo areaVo) {
        log.info("-------------areaVo={}", areaVo);

        return RespData.successResp(null);
    }


    @ResponseBody
    @RequestMapping(value = "/validate", method = RequestMethod.GET)
    public RespData<Object> validate(@Validated @ModelAttribute @RequestBody AreaVo areaVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            for (ObjectError error : bindingResult.getAllErrors()) {
                return RespData.failedResp(error.getDefaultMessage());
            }
        }

        return RespData.successResp(null);
    }

    @ResponseBody
    @RequestMapping(value = "/validate2", method = RequestMethod.GET)
    public RespData<Object> validate2(@ApiParam(value = "参数data2") @RequestParam("data2") String data) {


        return RespData.successResp(null);
    }


    @ResponseBody
    @RequestMapping(value = "/getArea1", method = RequestMethod.GET)
    public RespData<Object> getArea(@ApiParam(value = "id") @RequestParam("id") Long id, UserInfo userInfo) {

        System.out.println(userInfo);

        return RespData.successResp(areaMapper.getById(id));
    }


    @ResponseBody
    @RequestMapping(value = "/getArea", method = RequestMethod.GET)
    public RespData<Object> getArea(@ApiParam(value = "id") @RequestParam("id") Long id) {

        return RespData.successResp(areaMapper.getById(id));
    }


    @ApiOperation(value = "下载文件", notes = "")
    @RequestMapping(value = "/down", method = RequestMethod.GET)
    public void down(@ApiParam(value = "文件ID", required = true) @RequestParam("id") Long id, HttpServletResponse response) throws IOException {
        // 这里 FileInputStream 应该根据文件ID去文件服务器取,FileInputStream需要自己关闭
        try (FileInputStream fis = new FileInputStream("D:/33.txt")) {
            FileDownUtils.downFile(fis, "345.txt", response, false);
        }
    }


    @ApiOperation(value = "上传文件", notes = "")
    @ResponseBody
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public RespData upload(@ApiParam(value = "上传文件", required = true) @RequestParam("file") MultipartFile file)
            throws IllegalStateException, IOException {

        // 50M
        if (file.getSize() > 50 * FileDownUtils.MB) {
            throw new MaxUploadSizeExceededException(50 * FileDownUtils.MB);
        }


        file.transferTo(new File("D:/tmp/" + file.getOriginalFilename()));


        return RespData.successResp(null);

    }


}
